package com.gps.web.dao.sheet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gps.web.comm.dao.BaseDaoBean;
import com.gps.web.entity.sheets.VehicleRegist;

@Repository
public class VehicleRegistDaoBean extends BaseDaoBean<VehicleRegist, String> implements
		VehicleRegistDao {

	@Override
	@Transactional
	public List<VehicleRegist> getVehicleRegistByPage(Map<String, Object> map) {
		return findByCriteria(getCriteria(map));
	}
	
	private Criteria getCriteria(Map<String, Object> map) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(VehicleRegist.class);
		
		if(!StringUtils.isBlank((String)map.get("registDate"))){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			try {
				criteria.add(Restrictions.eq("registDate", df.parse(map.get("registDate").toString())));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(!StringUtils.isBlank((String)map.get("licensePlateNum"))){
			criteria.add(Restrictions.eq("licensePlateNum", map.get("licensePlateNum").toString()));
		}
		if(!StringUtils.isBlank((String)map.get("deviceID"))){
			criteria.add(Restrictions.eq("deviceID", map.get("deviceID").toString()));
		}
		if(!StringUtils.isBlank((String)map.get("carNum"))){
			criteria.add(Restrictions.eq("carNum", map.get("carNum").toString()));
		}
		if(!StringUtils.isBlank((String)map.get("motorcade"))){
			criteria.add(Restrictions.eq("motorcade", map.get("motorcade").toString()));
		}
		
		return criteria;
	}
}
